<!-- 手机注册 -->
<template>
  <div class="phone">
    <el-form
      :model="ruleForm2"
      status-icon
      :rules="rules2"
      ref="ruleForm2"
      label-width="0"
      class="demo-ruleForm"
    >
      <!-- 用户名 -->
      <el-form-item prop="username">
        <el-input
          v-model="ruleForm2.username"
          auto-complete="off"
          placeholder="请输入用户名"
        ></el-input>
      </el-form-item>

      <!-- 手机号 -->
      <el-form-item prop="tel">
        <el-input
          v-model="ruleForm2.tel"
          auto-complete="off"
          placeholder="请输入手机号"
        ></el-input>
      </el-form-item>

      <!-- 验证码 -->
      <el-form-item prop="smscode" class="code">
        <el-input v-model="ruleForm2.smscode" placeholder="验证码"></el-input>
        <el-button
          type="primary"
          :disabled="isDisabled"
          @click="sendCode"
          class="sendCode"
          >{{ buttonText }}</el-button
        >
      </el-form-item>

      <!-- 密码 -->
      <el-form-item prop="pass">
        <el-input
          type="password"
          v-model="ruleForm2.pass"
          auto-complete="off"
          placeholder="输入密码"
        ></el-input>
      </el-form-item>

      <!-- 确认密码 -->
      <el-form-item prop="checkPass">
        <el-input
          type="password"
          v-model="ruleForm2.checkPass"
          auto-complete="off"
          placeholder="确认密码"
        ></el-input>
      </el-form-item>

      <!-- 注册按钮 -->
      <el-form-item class="registerBtn">
        <el-button
          type="primary"
          @click="submitForm('ruleForm2')"
          style="width: 100%"
          >注册</el-button
        >
        <p class="login" @click="gotoLogin">已有账号？立即登录</p>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getCode } from "../../api/testApi/account";
import { goRegister } from "../../api/testApi/account";

export default {
  name: "Register",
  data() {
    // 验证用户名是否合法
    let checkUsername = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else if (!this.checkName(value)) {
        callback(new Error("用户名必须使用字母或者数字"));
      } else {
        callback();
      }
    };
    // 验证手机号是否合法
    let checkTel = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入手机号码"));
      } else if (!this.checkMobile(value)) {
        callback(new Error("手机号码不合法"));
      } else {
        callback();
      }
    };
    // 验证码是否为空
    let checkSmscode = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入手机验证码"));
      } else {
        callback();
      }
    };
    // 验证密码
    let validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm2.checkPass !== "") {
          this.$refs.ruleForm2.validateField("checkPass");
        }
        callback();
      }
    };
    // 二次验证密码
    let validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm2.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm2: {
        username: "",
        pass: "",
        checkPass: "",
        tel: "",
        smscode: "",
      },
      rules2: {
        username: [{ validator: checkUsername, trigger: "change" }],
        pass: [{ validator: validatePass, trigger: "change" }],
        checkPass: [{ validator: validatePass2, trigger: "change" }],
        tel: [{ validator: checkTel, trigger: "change" }],
        smscode: [{ validator: checkSmscode, trigger: "change" }],
      },
      buttonText: "发送验证码",
      isDisabled: false, // 是否禁止点击发送验证码按钮
      flag: true,
    };
  },
  methods: {
    // 发送验证码
    async sendCode() {
      let tel = this.ruleForm2.tel;
      if (this.checkMobile(tel)) {
        console.log(tel);
        let data = {
          mobile: "tel",
        };
        let res = await getCode(data);
        console.log(res);
        this.ruleForm2.smscode = res.data.result.code;

        // 一分钟只能获取一次验证码
        let time = 60;
        this.buttonText = "已发送";
        this.isDisabled = true;
        if (this.flag) {
          this.flag = false;
          let timer = setInterval(() => {
            time--;
            this.buttonText = time + " 秒";
            if (time === 0) {
              clearInterval(timer);
              this.buttonText = "重新获取";
              this.isDisabled = false;
              this.flag = true;
            }
          }, 1000);
        }
      }
    },
    // 提交注册
    async submitForm() {
      let data = {
        name: this.ruleForm2.username,
        password: this.ruleForm2.pass,
        phone: this.ruleForm2.tel,
      };
      let res = await goRegister(data);
      console.log(res);
      if (res.status == 200) {
        alert("注册成功，现在为您自动跳转到登录页");
        this.$router.push("/login");
      } else {
        alert("注册失败，请检查您填写的信息");
      }
    },
    // 进入登录页
    gotoLogin() {
      this.$router.push({
        path: "/login",
      });
    },
    // 验证用户名规范(用户名只允许使用字母或者数字)
    checkName(str) {
      let re = /^[a-z0-9]+$/i;
      if (re.test(str)) {
        return true;
      } else {
        return false;
      }
    },
    // 验证手机号规范
    checkMobile(str) {
      let re = /^1\d{10}$/;
      if (re.test(str)) {
        return true;
      } else {
        return false;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.phone {
  .demo-ruleForm {
    ::v-deep .el-form-item {
      width: 400px;
      margin: 24px auto 0;
    }
    ::v-deep .el-input__inner {
      font-size: 14px;
      color: #333;
      line-height: 20px;
      border: none;
      padding-bottom: 4px;
      border-radius: 0;
      width: 100%;
      border-bottom: 1px solid rgb(204, 204, 204);
      &:focus {
        outline: medium;
      }
    }
    ::v-deep .code {
      position: relative;
      .sendCode {
        position: absolute;
        right: 0;
        bottom: 5px;
        width: 100px;
        height: 26px;
        // background-color: #f7f7f7;
        border: 1px solid #ccc;
        cursor: pointer;
        // color: #999;
        border-radius: 2px;
        font-size: 12px;
        display: flex;
        justify-content: center;
        align-items: center;
      }
    }
    .registerBtn {
      button {
        border-radius: 0;
        font-size: 16px;
        color: #fff;
        font-weight: 700;
      }
      p {
        text-align: right;
        font-size: 14px;
        color: #0096ff;
        cursor: pointer;
        padding-bottom: 25px;
      }
    }
  }
}
</style>
